<?php
/**
 * 《固定资产》实现类
 *
 * @author 孙晓晔
 * @version $Id: Equipment.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Equipment extends InfoUtil {
	public $tbl = Constant::tbl_equipment;

	public $status = array(
		'-' => '请选择',
		'1' => '闲置未用',
		'2' => '正在使用',
	
		'9' => '已经报废',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '名称',
		'dept' => '部门',
		'emp' => '员工',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$category = $this->_category();
		$controller->assign_by_ref('category_option', $category);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/equipment/find');
		return true;
	}

	function _category() {
		$key = "equipment.category_";
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT category FROM %s WHERE length(category) > 0";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$ret[$row['category']] = $row['category'];
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function _type() {
		$key = "equipment.type_";
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT type FROM %s WHERE length(type) > 0";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$ret[$row['type']] = $row['type'];
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_category($controller, $request) {
		if (!empty($request['category'])) {
			$category = $request['category'];
		}

		if (!empty($request['category_s'])) {
			$category = $request['category_s'];
		}

		if (!empty($request['category_t'])) {
			$category = $request['category_t'];
		}

		if (empty($category) || $category == '-') {
			$data[] = array('code'=>'-', 'name'=>'请选择');
			echo json_encode($data);
			return false;
		}

		$key = "equipment.category_" . substr(md5($category), 0, 10);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				echo json_encode($ret);
				return true;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT type FROM %s WHERE category = '%s'";
		$sql = sprintf($fmt, $this->tbl, $category);
		debug($sql);
		$result = $db->query($sql);
		$ret[] = array('code'=>'-', 'name'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$row['code'] = $row['type'];
			$row['name'] = $row['type'];

			$ret[] = $row;
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("3 day"));
		}

		echo json_encode($ret);
		return true;
	}

	/**
	 * 清除缓存
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_category_type($controller, $request) {

		$category = $this->_category();
		while (list($k, $v) = each($category)) {
			$key = "equipment.category_" . substr(md5($k), 0, 10);
			clean($key);
		}

		$key = "equipment.category_";
		clean($key);

		$key = "equipment.type_";
		clean($key);

		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'dept') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = Name::dept($row['dept']);
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$code_s = $request['code_s'];
		$name_s = $request['name_s'];

		$category_s = $request['category_s'];
		$type_s = $request['type_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($code_s)) {
			$where .= " AND code = '" . $code_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND name like '%" . $name_s . "%'";
		}

		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		if ($this->_audit('153')) {
			return " office = '1'";
		}
		
		if ($this->_audit('154')) {
			return " office = '2'";
		}

		$emp = $_SESSION['id'];

		return " oper = '" . $emp . "'";
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/equipment/list');
		return true;
	}

	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {

			$row['emp_t'] = Name::emp($row['emp']);

			$row['status_t'] = $this->status[$row['status']];

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'info/equipment/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function my($controller, $request) {

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/equipment/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$code = $request['code'];
		$name = $request['name'];

		$category = addslashes($request['category']);
		$type = addslashes($request['type']);

		$purpose = addslashes($request['purpose']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, category, type, purpose, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $name, $category, $type, $purpose, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);

		$id = $db->insert_id();
		if (empty($code)) {
			$code = $this->code();
		}

		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $id);
		$db->query($sql);

		$request['id_s'] = $id;
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function code() {
		$ret = '';
		$dd = sprintf("%s", date('ymd00'));

		do {
			$dd++;
			$dd = sprintf("%08d", $dd);
			$ret = "B" . $dd;
		} while ($this->code_exist($ret));

		return $ret;
	}

	function code_exist($code) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/equipment/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$code = $request['code'];
		$name = $request['name'];

		$category = addslashes($request['category']);
		$type = addslashes($request['type']);

		$purpose = addslashes($request['purpose']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET code = '%s', name = '%s', category = '%s', type = '%s', content = '%s', purpose = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $name, $category, $type, $content, $purpose, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function discard($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/equipment/discard');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function discardPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = sprintf("\n%s\n%s 报废", addslashes($request['content']), Name::emp($_SESSION['id']));

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '9', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['emp_t'] = Name::emp($row['emp']);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/equipment/view');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function emp($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/equipment/emp');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function empPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$content = sprintf("\n%s\n%s 分配", addslashes($request['content']), Name::emp($_SESSION['id']));

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', content = concat(content, '%s'), emp = '%s', status = '2', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $content, $emp, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/equipment/copy');
		return true;
	}

	function idle($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = sprintf("\n%s 操作", Name::emp($_SESSION['id']));

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), emp = '', status = '1', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $content, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function cal($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/equipment/cal');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function calPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$content = addslashes($request['content']);

		$where = $this->_cal_where($emp, $t1, $t2);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s a, %s b SET a.dept = b.dept WHERE a.emp = b.id %s";
		$sql = sprintf($fmt, $this->tbl, Constant::tbl_emp, $where);
		debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _cal_where($emp, $date1, $date2) {
		$where = "";
		if (!empty($emp)) {
			$where .= " AND a.emp = '" . $emp . "'";
		}

		if (!empty($date1)) {
			$where .= " AND a.createtime >= '" . $date1 . "'";
		}

		if (!empty($date2)) {
			$where .= " AND a.createtime <= '" . $date2 . "'";
		}

		return $where;
	}

	function z($controller, $request) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, Constant::tbl_computer);
		echo $sql;
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {

			$content = sprintf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", $row['cpu'], $row['mb'], $row['display'], $row['memory'], $row['disk'], $row['cdrom'], $row['monitor'], $row['box'], $row['kb'], $row['mouse'], $row['content']);
			$fmt = "INSERT INTO %s(code, name, category, type, purpose, content, emp, status, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')";
			$sql = sprintf($fmt, $this->tbl, $row['code'], '电脑', '3C', '电脑', $row['purpose'], $content, $row['emp'], $row['status'], $row['createtime'], $row['modifytime']);
			// echo $sql;
			$db->query($sql);
		}

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}
}
?>